package com.ruoyi.financial.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.financial.domain.FThing;
import com.ruoyi.financial.service.IFThingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 物品管理Controller
 * 
 * @author xunzhe
 * @date 2024-01-23
 */
@Controller
@RequestMapping("/financial/fThing")
public class FThingController extends BaseController
{
    private String prefix = "financial/fThing";

    @Autowired
    private IFThingService fThingService;

    @RequiresPermissions("financial:fThing:view")
    @GetMapping()
    public String fThing()
    {
        return prefix + "/fThing";
    }

    /**
     * 查询物品管理树列表
     */
    @RequiresPermissions("financial:fThing:list")
    @PostMapping("/list")
    @ResponseBody
    public List<FThing> list(FThing fThing)
    {
        List<FThing> list = fThingService.selectFThingList(fThing);
        return list;
    }

    /**
     * 导出物品管理列表
     */
    @RequiresPermissions("financial:fThing:export")
    @Log(title = "物品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FThing fThing)
    {
        List<FThing> list = fThingService.selectFThingList(fThing);
        ExcelUtil<FThing> util = new ExcelUtil<FThing>(FThing.class);
        return util.exportExcel(list, "物品管理数据");
    }

    /**
     * 新增物品管理
     */
    @GetMapping(value = { "/add/{thingId}", "/add/" })
    public String add(@PathVariable(value = "thingId", required = false) Long thingId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(thingId))
        {
            mmap.put("fThing", fThingService.selectFThingByThingId(thingId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存物品管理
     */
    @RequiresPermissions("financial:fThing:add")
    @Log(title = "物品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FThing fThing)
    {
        if(fThing.getParentId()==null){
            fThing.setParentId(0L);
        }

        List<FThing> list = fThingService.checkName(fThing.getThingName());
        for(FThing data : list){
            if(data.getParentId().longValue() == fThing.getParentId().longValue()){
                return AjaxResult.warn("物品名称重复");
            }
        }

        fThing.setCreateBy(getLoginName());
        return toAjax(fThingService.insertFThing(fThing));
    }

    /**
     * 修改物品管理
     */
    @RequiresPermissions("financial:fThing:edit")
    @GetMapping("/edit/{thingId}")
    public String edit(@PathVariable("thingId") Long thingId, ModelMap mmap)
    {
        FThing fThing = fThingService.selectFThingByThingId(thingId);
        mmap.put("fThing", fThing);
        return prefix + "/edit";
    }

    /**
     * 修改保存物品管理
     */
    @RequiresPermissions("financial:fThing:edit")
    @Log(title = "物品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FThing fThing)
    {
        List<FThing> list = fThingService.checkName(fThing.getThingName());
        for(FThing data : list){
            if(data.getThingId().longValue() != fThing.getThingId().longValue()){
                if(data.getParentId().longValue() == fThing.getParentId().longValue()){
                    return AjaxResult.warn("物品名称重复");
                }
            }
        }

        fThing.setUpdateBy(getLoginName());
        return toAjax(fThingService.updateFThing(fThing));
    }

    /**
     * 删除
     */
    @RequiresPermissions("financial:fThing:remove")
    @Log(title = "物品管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{thingId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("thingId") Long thingId)
    {
        int i = fThingService.checkCount(thingId);
        if(i > 0){
            return AjaxResult.warn("存在下级节点,不允许删除");
        }
        return toAjax(fThingService.deleteFThingByThingId(thingId));
    }

    /**
     * 选择物品管理树
     */
    @GetMapping(value = { "/selectFThingTree/{thingId}", "/selectFThingTree/","/selectFThingTree/{thingId}/{type}" })
    public String selectFThingTree(@PathVariable(value = "thingId", required = false) Long thingId,@PathVariable(value = "type", required = false) Long type, ModelMap mmap)
    {
        if (StringUtils.isNotNull(thingId))
        {
            mmap.put("fThing", fThingService.selectFThingByThingId(thingId));
        }
        mmap.put("type", type);
        return prefix + "/tree";
    }

    /**
     * 加载物品管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = fThingService.selectFThingTree();
        return ztrees;
    }

    @GetMapping(value = { "/selectFThingCategoryTree/{thingId}", "/selectFThingCategoryTree/" })
    public String selectFThingCategoryTree(@PathVariable(value = "thingId", required = false) Long thingId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(thingId))
        {
            mmap.put("fThing", fThingService.selectFThingByThingId(thingId));
        }
        return prefix + "/categoryTree";
    }

    @GetMapping("/categoryTreeData")
    @ResponseBody
    public List<Ztree> categoryTreeData()
    {
        List<Ztree> ztrees = fThingService.selectFThingCategoryTree();
        return ztrees;
    }

    @GetMapping("/checkCount/{thingId}")
    @ResponseBody
    public AjaxResult checkCount(@PathVariable("thingId") Long thingId) {
        int i = fThingService.checkCount(thingId);
        return AjaxResult.success("成功",i);
    }



}
